<template>
  <div class="tab-title-page goods_list">
    <div class="common_form_search">
      <el-form ref="searchForm" :inline="true" :model="searchForm">
        <el-form-item label="工单号">
          <el-input v-model="searchForm.work_order" placeholder="请输入工单号" clearable />
        </el-form-item>
        <el-form-item label="投诉类型">
          <el-select v-model="searchForm.suggest_type" placeholder="全部" clearable>
            <el-option v-for="item in typeList" :key="item.id" :label="item.type_name" :value="item.type_name" />
          </el-select>
        </el-form-item>
        <el-form-item label="工单状态">
          <el-select v-model="searchForm.status" placeholder="全部" clearable>
            <el-option label="待处理" value="1" />
            <el-option label="处理中" value="2" />
            <el-option label="已处理" value="3" />
            <el-option label="已撤销" value="4" />
            <el-option label="已超时" value="5" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="工单等级">
          <el-select
            @change="selectGrade" v-model="searchForm.grade" placeholder="全部" clearable>
            <el-option label="普通" value="1"></el-option>
            <el-option label="紧急" value="2"></el-option>
            <el-option label="严重" value="3"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="创建时间">
          <my-date-picker ref="myDatePicker" />
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" @click="search()">查询</el-button>
          <el-button size="small" @click="reset()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-tabs v-model="activeName" class="flex-1 nav_tabs" @tab-click="handleClick">
      <el-tab-pane :label="`全部工单(${count.grade_total})`" name="0" />
      <el-tab-pane :label="`普通工单(${count.ordinary_grade_total})`" name="1" />
      <el-tab-pane :label="`紧急工单(${count.urgent_grade_total})`" name="2" />
      <el-tab-pane :label="`严重工单(${count.serious_grade_total})`" name="3" />
    </el-tabs>
    <el-divider class="middle_divider" />
    <div class="child_page">
      <div class="flex align-center table-top-btns">
        <h5 class="flex align-center">
          <svg-icon style="margin-right: 15px" icon-class="list" :size="15" />数据列表
        </h5>
      </div>
      <el-table v-show="tableData.length > -1" :data="tableData" border>
        <el-table-column fixed="left" align="center" prop="id" label="ID" width="60" />
        <el-table-column align="center" prop="work_order" label="工单号" width="150" />
        <el-table-column align="center" prop="user_mobile" label="联系手机" width="120" />
        <el-table-column align="center" prop="suggest_type" label="投诉类型" width="120" />
        <el-table-column align="center" prop="suggest_cate" label="投诉类别" width="110">
          <template slot-scope="scope">
            <p class="text-ellipsis">
              {{ scope.row.suggest_cate == 1 ? "商品名称" : scope.row.suggest_cate == 2 ? "商家名称" : "订单编号" }}
            </p>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="suggest_title" label="类别内容" />
        <el-table-column align="center" label="工单等级" width="150">
          <template slot-scope="scope">
            <span v-if="scope.row.grade">
              <span class="before" :class=" scope.row.grade == 1 ? 'primary' : scope.row.grade == 2 ? 'warning' : 'danger' " />
              {{ scope.row.grade == 1 ? "普通" : scope.row.grade == 2 ? "紧急" : "严重" }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="工单状态" width="140">
          <template slot-scope="scope">
            <!-- 状态 1待处理 2处理中 3已处理 4已撤销 5已超时 -->
            <el-tag v-if="scope.row.status" :type=" scope.row.status == '3' ? 'success' : scope.row.status == '1' ? '' : scope.row.status == '2' ? 'warning' : 'danger' ">
              {{ scope.row.status == "1" ? "待处理" : scope.row.status == "2" ? "处理中" : scope.row.status == "3" ? "已处理" : scope.row.status == "4" ? "已撤销" : "已超时" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="create_time" label="创建时间" width="100" />
        <el-table-column align="center" prop="end_time" label="截止期限" width="100" />
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="check(scope.row.id)">查看</el-button>
            <el-button v-if="scope.row.status != '3' && $hasMethod('#intervene')" type="warning" size="mini" @click="intervene(scope.row.id)">介入</el-button>
            <el-button v-if="$hasMethod('#note')" type="primary" size="mini" @click="remark(scope.row.id)">备注</el-button>
          </template>
        </el-table-column>
      </el-table>
      <custom-page ref="customPage" :total="total" @getList="getList" />
    </div>
    <dispose ref="disposes" :suggest-id="suggestId" />
    <note-records ref="records" :suggest-id="suggestId" />
  </div>
</template>

<script>
import { getSuggestType, complaintList } from '@/api/complaint'
import customPage from '@/components/customPage'
import myDatePicker from '@/components/myDatePicker'
import dispose from './dailog/dispose'
import noteRecords from './dailog/noteRecords'
export default {
  components: { customPage, myDatePicker, noteRecords, dispose },
  data() {
    return {
      searchForm: {
        work_order: '',
        suggest_type: '',
        status: '',
        grade: '',
        create_time: ''
      },
      activeName: '0',
      suggestId: '',
      count: {
        grade_total: 0,
        ordinary_grade_total: 0,
        urgent_grade_total: 0,
        serious_grade_total: 0
      },
      typeList: [],
      total: 0,
      tableData: []
    }
  },
  mounted() {
    this.getType()
    this.getList()
  },
  methods: {
    check(id) {
      this.suggestId = id
      this.$router.push({ name: 'complaintDetail', params: { id }})
    },
    intervene(id) {
      this.suggestId = id
      this.$refs.disposes.dialogVisible = true
    },
    remark(id) {
      this.suggestId = id
      this.$refs.records.dialogVisible = true
    },
    getType() {
      getSuggestType().then((res) => {
        this.typeList = res
      })
    },
    // 获取数据
    getList(flag) {
      // flag为true, 则不刷新表格
      const { page, pageSize } = this.$refs.customPage
      if (!flag) {
        this.tableData = []
      }
      complaintList({
        page,
        pageSize,
        ...this.searchForm,
        grade: this.activeName
      }).then((res) => {
        this.tableData = res.data || []
        this.total = res.total
        for (const key in this.count) {
          this.count[key] = res.count[key]
        }
      })
    },
    // 查询
    search() {
      const pickerVal = this.$refs.myDatePicker.getVal()
      if (pickerVal !== 'error') {
        this.searchForm.create_time = pickerVal
      }
      this.$refs.customPage.page = 1
      this.getList()
    },
    // selectGrade(val){
    //   console.log('val',val)
    //   this.searchForm.grade = val
    // },
    // 重置
    reset() {
      if (this.$refs.myDatePicker) {
        this.$refs.myDatePicker.reset()
      }
      this.searchForm = {
        work_order: '',
        suggest_type: '',
        status: '',
        grade: ''
      }
      this.getList()
    },
    handleClick(tab) {
      this.activeName = tab.name
      this.$refs.customPage.page = 1
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-button--warning.is-plain {
  color: #ff9f38;
  background: #fff;
  border-color: #ff9f38;
}
.el-icon-upload2 {
  font-size: 18px;
}
.dialog {
  text-align: center;
}
.goods_info {
  .info {
    margin-left: 10px;
    p {
      color: #666666;
    }
    p:first-child {
      color: #999999;
    }
  }
}
.activity {
  line-height: 24px;

  span {
    width: 20px;
    height: 20px;
    margin-left: 10px;
  }
}
.child_page {
  padding: 30px;
}
.common_form_search {
  padding-top: 15px;
  padding-left: 15px;
  padding-right: 15px;
  background-color: #ffffff;
  border-bottom: 1px solid #eeeeee;
}
.before {
  display: inline-block;
  width: 7px;
  height: 7px;
  background: #2993ff;
  border-radius: 50%;
}
.primary {
  background: #2993ff;
}
.warning {
  background: #ff9f38;
}
.danger {
  background: #ff4146;
}
</style>
